<template>
  <div class="login">
    <div class="login-box">
      <el-image style="width: 404px; height: 644px" :src="main_img" fit="cover" />
      <div class="login-form">
        <h3 class="title">身份验证</h3>
        <el-collapse-transition>
          <el-form v-show="show" ref="codeFormRef" :model="codeForm" :rules="codeRules">
            <el-form-item prop="phone">
              <el-input v-model="codeForm.phone" size="large" auto-complete="off" placeholder="请输入手机号">
                <template #prefix>
                  <el-icon class="el-input__icon" :size="40">
                    <Iphone />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code">
              <el-input v-model="codeForm.code" size="large" auto-complete="off" placeholder="图形验证码" style="width: 63%"
                @keyup.enter="codeFormSubmit">
                <template #prefix>
                  <el-icon class="el-input__icon" :size="40">
                    <Key />
                  </el-icon>
                </template>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode()" class="login-code-img" />
              </div>
            </el-form-item>
            <el-button size="large" color="#D6000F" style="width:100%;margin-top: 20px;" :loading="sendPhoneCode" 
              @click.prevent="codeFormSubmit">发送验证码</el-button>
          </el-form>
        </el-collapse-transition>
        <el-collapse-transition>
          <el-form v-show="!show" ref="resetFormRef" :model="resetForm" :rules="resetRules">
            <el-form-item prop="phone" v-show="false"></el-form-item>
            <el-form-item prop="code">
              <el-input v-model="resetForm.code" size="large" autocomplete="off" placeholder="请输入短信验证码"
                style="width: 100%">
                <template #prefix>
                  <el-icon class="el-input__icon" :size="40">
                    <Key />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="pwd">
              <el-input v-model="resetForm.pwd" type="password" autocomplete="off" size="large" placeholder="请输入密码"
                show-password>
                <template #prefix>
                  <el-icon class="el-input__icon" :size="40">
                    <Lock />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="rePwd">
              <el-input v-model="resetForm.rePwd" type="password" autocomplete="off" size="large" placeholder="请输入确认密码"
                show-password>
                <template #prefix>
                  <el-icon class="el-input__icon" :size="40">
                    <Lock />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-button size="large" color="#D6000F" style="width:100%;margin-top: 20px;" :loading="resetLoading"
              @click.prevent="resetFormSubmit">确定</el-button>
          </el-form>
        </el-collapse-transition>
      </div>
    </div>
  </div>
</template>
<script setup>
import { getCodeImg } from "@/api/login";
import { appLoginSendPhonePost, appLoginResetPwdPost } from "@/api/forgetPassword";
import main_img from "@/assets/images/main_img.png";
const router = useRouter();
const baseConfig = reactive({
  systemTitle: import.meta.env.VITE_APP_SYSTEM_TITLE,
  copyright: `Copyright © ${import.meta.env.VITE_APP_START_YEAR}-${new Date().getFullYear()} ${import.meta.env.VITE_APP_COPYING} All Rights Reserved.`
})
const show = ref(true)
const codeUrl = ref("");
function getCode (i) {
  getCodeImg().then(res => {
    codeUrl.value = "data:image/gif;base64," + res.img;
    codeForm.uuid = res.uuid;
  });
}
const codeForm = reactive({ //  - appLoginSendPhonePost body查询参数 appLoginSendPhonePostBody -body
  phone: '', // 手机号 string-body
  code: '', // 验证码（和登录一样） string-body
  uuid: '' // 验证码唯一标示（和登录一样） string-body
})
const codeRules = {
  phone: [
    { required: true, trigger: "blur", message: "请输入手机号" },
    { pattern: /^1[3456789]\d{9}$/, trigger: "blur", message: "手机号格式错误" }
  ],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}
getCode()
const sendPhoneCode = ref(false)
const codeFormRef = ref(null)
const codeFormSubmit = () => {
  codeFormRef.value.validate((valid) => {
    if (valid) {
      sendPhoneCode.value = true
      // 发送验证码
      appLoginSendPhonePost({ body: codeForm }).then(res => {
        resetForm.phone = codeForm.phone
        sendPhoneCode.value = false
        show.value = false
      }).catch(() => {
        getCode()
        sendPhoneCode.value = false
      })
    }
  })
}
const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (!/^(?=.*\d)(?=.*[a-zA-Z]).{6,18}$/.test(value)) {
    callback(new Error('密码至少有字母+数字，长度6 ~ 18位'))
  } else {
    if (resetForm.rePwd !== '') {
      if (!resetFormRef.value) return
      resetFormRef.value.validateField('rePwd')
    }
    callback()
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请重新输入密码'))
  } else if (value !== resetForm.pwd) {
    callback(new Error("两个输入不匹配！"))
  } else {
    callback()
  }
}
const resetForm = reactive({ //  - appLoginResetPwdPost body查询参数 appLoginResetPwdPostBody -body
  phone: '', // 手机号 string-body
  code: '', // 手机验证码 string-body
  pwd: '', // 密码 string-body
  rePwd: '' // 确认密码 string-body
})
const resetRules = {
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
  pwd: [{ required: true, trigger: "blur", validator: validatePass }],
  rePwd: [{ required: true, trigger: "blur", validator: validatePass2 }]
}
const resetLoading = ref(false)
const resetFormRef = ref(null)
const resetFormSubmit = () => {
  resetFormRef.value.validate((valid) => {
    if (valid) {
      // 重置密码
      resetLoading.value = true
      appLoginResetPwdPost({ body: resetForm }).then(res => {
        resetLoading.value = false
        router.push({ path: "/" });
      }).catch(() => {
        show.value = true
        resetLoading.value = false
      })
    }
  })
}
</script>
<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.png");
  background-size: cover;
  padding: 130px 0;

}

.title {
  margin: 0px auto 37px auto;
  text-align: center;
  font-weight: bold;
  font-size: 26px;
  color: var(--jbc-font-color);
  line-height: 35px;
}

.login-form {
  background-color: #ffffff;
  width: 621px;
  padding: 63px 87px 0;

  .el-form-item--default {
    margin-bottom: 24px;
  }

  .el-input {
    height: 55px;

    input {
      height: 55px;
    }

    &__icon {
      padding-right: 10px;
      margin-right: 10px;
      border-right: 1px solid #EBEBEB;
    }
  }

  .link-type {
    color: var(--jbc-color);
    text-decoration-line: underline;
    font-size: var(--el-font-size-base);
  }

  .el-button--large {
    --el-button-size: 55px;
    --el-border-radius-base: 0;
    --el-font-size-base: 18px;
    padding: 12px 19px;
    width: 100%;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 55px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 55px;
  padding-left: 12px;
}

.login-box {
  width: 1025px;
  display: flex;
  align-items: stretch;
}
</style>